package com.dream.service.user.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.dream.common.model.response.BaseListResponse;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.response.ListModel;
import com.dream.service.designer.api.erp.DesignerErpService;
import com.dream.service.designer.domain.model.DesignerModel;
import com.dream.service.user.api.UserFollowService;
import com.dream.service.user.dao.UserFollowDao;
import com.dream.service.user.domain.entity.IsFollowCollect;
import com.dream.service.user.domain.entity.UserFollow;
import com.dream.service.user.domain.model.UserFollowModel;
import com.dream.service.user.util.ConfigUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by houlijiang on 16/5/18.
 * <p>
 * 设计师关注列表
 */
public class UserFollowServiceImpl implements UserFollowService {

    private UserFollowDao userFollowDao;

    public UserFollowDao getUserFollowDao() {
        return userFollowDao;
    }

    @Autowired
    public void setUserCollectionDao(UserFollowDao userFollowDao) {
        this.userFollowDao = userFollowDao;
    }

    //service
    DesignerErpService designerErpService;

    public DesignerErpService getProductWrapperService() {
        return designerErpService;
    }

    public void setDesignerErpService(DesignerErpService designerErpService) {
        this.designerErpService = designerErpService;
    }

    @Override
    public BaseResponse<Boolean> addFollow(String pin, List<Long> designerList) {
        long startTime = System.currentTimeMillis();
        if(designerList == null || designerList.size()<=0 || StringUtils.isEmpty(pin)){
            BaseResponse<Boolean> baseResponse= new BaseResponse<Boolean>(ErrorCode.PARAM_ERROR);
            return baseResponse;
        }
        BaseResponse<Boolean> baseResponse = new BaseResponse<Boolean>();
        try {
            List<Long>  followList  = userFollowDao.selectByPINFollow(pin, designerList);
            for(Long l :followList){
                designerList.remove(l);
            }
            if(designerList.size()>0){
                int num = userFollowDao.addUserFollow(pin, designerList);
                if (num != designerList.size()) {
                    baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, true);
                }else {
                    baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, true);
                }
            }else {
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseResponse.packData(false, ErrorCode.DB_ERROR.getCode(), e.getMessage(), null, false);
        }
        baseResponse.setLatency(System.currentTimeMillis() - startTime);
        return baseResponse;
    }

    @Override
    public BaseResponse<Boolean> delFollow(String pin, List<Long> designerList) {
        long startTime = System.currentTimeMillis();
        BaseResponse<Boolean> baseResponse = new BaseResponse<Boolean>();
        try {
            int num = userFollowDao.delUserFollow(pin, designerList);
            if (num != designerList.size()) {
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, num > 0);
            } else {
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, num > 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseResponse.packData(false, ErrorCode.DB_ERROR.getCode(), e.getMessage(), null, false);
        }
        baseResponse.setLatency(System.currentTimeMillis() - startTime);
        return baseResponse;
    }

    @Override
    public BaseListResponse getFollowList(String pin, Integer pageNum, Integer pageSize) {
        long startTime = System.currentTimeMillis();
        BaseListResponse baseListResponse = new BaseListResponse();
        ListModel listModel = new ListModel();
        try {
            List<UserFollowModel> list = null;
            if (pageNum == null || pageSize == null) {
                list = userFollowDao.selectByPIN(pin, null, null);
            } else if(pageNum>0 && pageSize>=0){
                list = userFollowDao.selectByPIN(pin, (pageNum - 1) * pageSize, pageSize);
            }
            if (list == null) {
                baseListResponse.packData(false, ErrorCode.NOT_EXIST.getCode(), ErrorCode.NOT_EXIST.getError_msg(), null, null);
                baseListResponse.setLatency(System.currentTimeMillis() - startTime);
                return baseListResponse;
            }
            //最终结果
            List<UserFollow> userList = new ArrayList<UserFollow>();
            if (list.size() == 0) {
                listModel.setAmount(0);
                listModel.setList(userList);
                baseListResponse.packData(true, ErrorCode.OK.getCode(), "", null, listModel);
                baseListResponse.setLatency(System.currentTimeMillis() - startTime);
                return baseListResponse;
            }

            //取出设计师ID
            List<Integer> designerIdList = new ArrayList<Integer>();
            for (UserFollowModel m : list) {
                designerIdList.add(m.getDesigner_id());
            }
            //批量查询设计师信息
            BaseListResponse designerList = designerErpService.getDesignerInfoList(designerIdList);
            if (!designerList.isSuccess()) {
                System.out.println("get designer info failed");
                baseListResponse.packData(false, ErrorCode.NOT_EXIST.getCode(), ErrorCode.NOT_EXIST.getError_msg(), null, null);
                baseListResponse.setLatency(System.currentTimeMillis() - startTime);
                return baseListResponse;
            } else {
                List<DesignerModel> desiList = (List<DesignerModel>) designerList.getResult().getList();
                for (DesignerModel d : desiList) {
                    if(d.getStatus_code() != 100){
                        continue;
                    }
                    UserFollowModel temp = null;
                    for(UserFollowModel m : list){
                        if(m.getDesigner_id().intValue() == d.getDesigner_id()){
                            temp = m;
                            break;
                        }
                    }
                    if(temp == null){
                        continue;
                    }
                    UserFollow follow = new UserFollow();
                    follow.setId(temp.getId());
                    follow.setUserId(d.getDesigner_id());
                    follow.setName(d.getName());
                    follow.setUrl(ConfigUtil.getPro("DESIGNER_HOME_PAGE_PREFIX").concat(String.valueOf(temp.getDesigner_id())));
                    follow.setNickname(d.getNickname());
                    follow.setAvatar(d.getIcon());
                    follow.setCreateTime(temp.getCreate_time());
                    follow.setDescription(d.getDescribe());
                    userList.add(follow);

                    list.remove(temp);
                }
            }
            //获取用户总数,如果有些设计师查不到信息则这个总数会不准
            int amount = userFollowDao.followListCountByPIN(pin);
            listModel.setAmount(amount);
            listModel.setList(userList);
            baseListResponse.packData(true, ErrorCode.OK.getCode(), "", null, listModel);
        } catch (Exception e) {
            e.printStackTrace();
            baseListResponse.packData(false, ErrorCode.DB_ERROR.getCode(), e.getMessage(), null, null);
        }
        baseListResponse.setLatency(System.currentTimeMillis() - startTime);
        return baseListResponse;
    }

    /**
     * 是否关注
     *
     * @param pin
     * @param designerID
     * @return
     */
    @Override
    public BaseResponse<IsFollowCollect> isFollow(String pin, Long designerID) {
        long startTime = System.currentTimeMillis();
        if(designerID == null || StringUtils.isEmpty(pin)){
            BaseResponse<IsFollowCollect> baseResponse= new BaseResponse<IsFollowCollect>(ErrorCode.PARAM_ERROR);
            return baseResponse;
        }
        BaseResponse<IsFollowCollect> baseResponse = new BaseResponse<IsFollowCollect>();
        try {
            List<Long>  followList  = userFollowDao.selectByPINFollow(pin, Arrays.asList(designerID));
            if(followList!=null && followList.size()==1){
                IsFollowCollect isFollowCollect = new IsFollowCollect();
                isFollowCollect.setIsFC(true);
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, isFollowCollect);
            }else {
                IsFollowCollect isFollowCollect = new IsFollowCollect();
                isFollowCollect.setIsFC(false);
                baseResponse.packData(true, ErrorCode.OK.getCode(), "", null, isFollowCollect);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseResponse.packData(false, ErrorCode.DB_ERROR.getCode(), e.getMessage(), null, null);
        }
        baseResponse.setLatency(System.currentTimeMillis() - startTime);
        return baseResponse;
    }
}
